package fr.kraml;

import java.util.ArrayList;
import java.util.Iterator;

public class BDD {
	boolean isConnected;
	String username;
	String room_name;
	ArrayList<ASV> asv_cache;
	ArrayList<RoomMessage> room_messages;
	ArrayList<Participant> participants;
	
	public BDD() {
		isConnected = false;
		username = "";
		room_name = "";
		asv_cache = new ArrayList<ASV>();
		room_messages = new ArrayList<RoomMessage>();
		participants = new ArrayList<Participant>();
	}
	
	public void add_room_message(String from, String mess, String color){
		room_messages.add(new RoomMessage(null, from, mess, color));
	}
	
	public boolean isParticipant(String username) {
		Iterator<Participant> i = participants.iterator();
		while (i.hasNext()) {
			Participant p = (Participant) i.next();
			if (p.username.equals(username))
				return true;
		}
		return false;
	}

	public Participant getParticipant(String username) {
		Iterator<Participant> i = participants.iterator();
		while (i.hasNext()) {
			Participant p = (Participant) i.next();
			if (p.username.equals(username))
				return p;
		}
		return null;
	}

	public void changeParticipant(String pres_from, String show, String status,
			int age, String sex, String location, String avatar) {

		if (show.equals("chat")) {
			if (getParticipant(pres_from) == null) {
				Participant p = new Participant(null, pres_from, show, status,
						age,
						sex,
						location,
						avatar);
				participants.add(p);
			} else {
				Participant p = getParticipant(pres_from);
				p.show = show;
				p.status = status;
				p.age = age;
				p.sex = sex;
				p.location = location;				
			}
		} else if (show.equals("unavailable")) {
			if (isParticipant(pres_from)) {
				Participant p = getParticipant(pres_from);
				participants.remove(p);
			}
		}
	}

}
